/*
 * Copyright (c) Created by T C L on 2019.7.6.
 */

package cn.mumzone.pos.ui.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.mumzone.basic.loading.button.LoadingButton;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.CardReturnEntity;
import cn.mumzone.pos.model.ReturnEntity;
import cn.mumzone.pos.model.TimeCardEntity;
import cn.mumzone.pos.presenter.TimesCardReturnPresenter;
import cn.mumzone.pos.presenter.contract.TimesCardReturnContract;

public class TimesCardReturnFailedDialogFragment extends DialogFragment implements TimesCardReturnContract.View {

    private String cardId;
    private String price;
    private String residuePrice;
    private String goodsId;
    private String orderCode;
    private String returnMargin;
    private String errorMsg;

    @BindView(R.id.lb_re_return)
    LoadingButton lbReReturn;
    @BindView(R.id.tv_error_msg)
    TextView tvErrorMsg;

    private String memberPhone;
    private TimesCardReturnPresenter presenter;
    private TimeCardEntity timeCardEntity;

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(false);//点击外部不消失
        // 设置SelectPicPopupWindow弹出窗体的背景
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
        params.gravity = Gravity.CENTER;
        params.width = DisplayUtil.dip2px(getActivity(), 438);
        params.height = DisplayUtil.dip2px(getActivity(), 444);
        window.setAttributes(params);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new TimesCardReturnPresenter(getActivity(), this);
        cardId = getArguments().getString("cardId");
        price = getArguments().getString("price");
        residuePrice = getArguments().getString("residuePrice");
        goodsId = getArguments().getString("goodsId");
        orderCode = getArguments().getString("orderCode");
        returnMargin = getArguments().getString("returnMargin");
        memberPhone = getArguments().getString("memberPhone");
        errorMsg = getArguments().getString("errorMsg");
        timeCardEntity = (TimeCardEntity) getArguments().getSerializable("timeCardEntity");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialog_times_card_return_failed, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tvErrorMsg.setText(StringUtils.get(errorMsg,"您可以尝试重新退卡试试~"));
    }

    @OnClick(value = {R.id.iv_close, R.id.tv_do_not_return, R.id.lb_re_return})
    public void toggleEvent(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_do_not_return:
                dismiss();
                break;
            case R.id.lb_re_return:
                lbReReturn.startLoading("重新退卡");
                presenter.goReturnCard(cardId, price, residuePrice, goodsId, orderCode, returnMargin);
                break;
        }
    }

    @Override
    public void setReturnCardDetailData(CardReturnEntity cardReturnEntity) {
        //do nothing
    }

    @Override
    public void onReturnSuccess(List<ReturnEntity> returnEntities) {
        TimesCardReturnSuccessDialogFragment timesCardReturnSuccessDialogFragment =
                new TimesCardReturnSuccessDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("cardId", cardId);
        bundle.putSerializable("money", (Serializable) returnEntities);
        bundle.putString("memberPhone", memberPhone);
        bundle.putSerializable("timeCardEntity", timeCardEntity);
        timesCardReturnSuccessDialogFragment.setArguments(bundle);
        timesCardReturnSuccessDialogFragment.show(getActivity().getSupportFragmentManager(),
                "timesCardReturnSuccess");
        dismiss();
    }

    @Override
    public void onReturnFailed(String errorMsg) {
        TimesCardReturnFailedDialogFragment timesCardReturnFailedDialogFragment =
                new TimesCardReturnFailedDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("cardId", cardId);
        bundle.putString("price", price);
        bundle.putString("residuePrice", residuePrice);
        bundle.putString("goodsId", goodsId);
        bundle.putString("orderCode", orderCode);
        bundle.putString("returnMargin", returnMargin);
        bundle.putString("memberPhone", memberPhone);
        bundle.putSerializable("timeCardEntity", timeCardEntity);
        timesCardReturnFailedDialogFragment.setArguments(bundle);
        timesCardReturnFailedDialogFragment.show(getActivity().getSupportFragmentManager(),
                "timesCardReturnFailed");
        dismiss();
    }

    @Override
    public void setRealReturnMoney(String realMoney) {
        //do nothing
    }

    @Override
    public void showUpdateReturnAmount() {
        //do nothing
    }

    @Override
    public void goAuthView() {
//do nothing
    }

}
